package cn.edu.ecut.url;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class URLTest {

	public static void main(String[] args) throws Exception {
		
		String protocol = "http" ; // 协议
		String host = "www.baidu.com" ; // 主机(可能是IP地址或域名)
		int port = 80 ; // 端口
		String file = "/index.html" ; // 资源路径
		
		
		// 统一资源定位器 ， Uniform Resource Locator
		URL url = new URL( protocol , host , port , file ) ;
		System.out.println( url );
		
		InputStream in = url.openStream();
		OutputStream out = new FileOutputStream( "baidu.txt" );
		
		int n ;
		byte[] bytes = new byte[ 1024 ];
		while( ( n = in.read( bytes ) ) != -1 ) {
			out.write( bytes ,  0 ,  n );
		}
		
		out.close();
		in.close();

	}

}
